//
//  HXMaterialsVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/14.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXMaterialsVC.h"
#import "HX_Macro.h"
#import "HXMaterialerTCell.h"
#import "HXMatersModel.h"

@interface HXMaterialsVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *string;
/**
 *  模型数组源
 */
@property (nonatomic, strong) NSMutableArray *maters;
@end

@implementation HXMaterialsVC
- (NSMutableArray *)maters {
    if (!_maters) {
        _maters = [NSMutableArray array];
    }
    return _maters;
}
- (NSMutableArray *)string {
    if (!_string) {
        _string = [NSMutableArray array];
    }
    return _string;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.separatorStyle = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"HXMaterialerTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXMaterialerTCell"];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLORAUTO;
    self.title = @"作品详情";
    [self creatLeftBtn];
    if (self.netId) {
        NSArray *arrID = [self.netId componentsSeparatedByString:@"#"];
        for (NSString *str in arrID) {
           [self.string addObject:str];
        }
        self.ID = self.string[1];
    }
    [self getNetWorkingNetId:self.ID];
    [self tableView];
}

- (void)creatLeftBtn {
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(0, 0, 30, 30);
    [left setImage:[UIImage imageNamed:@"dish_collection"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(addLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *share = [UIButton buttonWithType:UIButtonTypeCustom];
    share.frame = CGRectMake(0, 0, 30, 30);
    [share addTarget:self action:@selector(addShare:) forControlEvents:UIControlEventTouchUpInside];
    [share setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:left];
    UIBarButtonItem *bar2 = [[UIBarButtonItem alloc] initWithCustomView:share];
    self.navigationItem.rightBarButtonItems = @[bar2, bar];
}
- (void)addLeftBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"dish_collection_p"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(addLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)removeBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"dish_collection"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(addLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)addShare:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(addShare:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(removeShareBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)removeShareBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"分享_p"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(removeShareBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(addShare:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)getNetWorkingNetId:(NSString *)netid {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"post_id=%@&methodName=ShequPostview&version=4.40&", netid];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        HXMatersModel *maters = [HXMatersModel ModelWithDic:dicData];
        [self.maters addObject:maters];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXMaterialerTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXMaterialerTCell"];
    cell.selectionStyle = NO;
    HXMatersModel *model = [self.maters firstObject];
    [cell.headerImg sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.detailLabel.text = model.content;
    [cell.personImg sd_setImageWithURL:[NSURL URLWithString:model.head_img]];
    cell.name.text = model.nick;
    cell.creatTime.text = model.create_time;
    cell.headerArr = model.like;
    cell.photo = model.relates;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 660;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
